#!/bin/bash
new_password='Lixinyi@123'

# 停止MySQL服务
systemctl stop mysqld

# 清空MySQL数据目录
rm -rf /var/lib/mysql/*

# 启动MySQL服务
systemctl start mysqld

# 检查MySQL服务启动是否成功
if [ $? -eq 0 ]; then
    # 获取旧密码
    oldpassword=$(grep 'password' /var/log/mysqld.log | tail -n 1 | awk -F 'localhost: ' '{print $2}')

    # 使用mysqladmin命令重置密码
    mysqladmin -uroot -p"$oldpassword" password "$new_password"

    # 检查mysqladmin命令是否执行成功
    if [ $? -eq 0 ]; then
        echo "密码重置为默认密码，正在登录..."
        mysql -uroot -p"$new_password"
    else
        echo "密码重置失败"
    fi
else
    echo "MySQL启动失败"
fi

